﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AFI.Speargun.Laptop.ServiceReference;

namespace AFI.Speargun.Laptop
{
    public partial class CheckingForm : Form
    {
        Dictionary<string, AssetStatus> assetStatusDict;
        Dictionary<string, CheckingResult> checkingResultDict;

        ServiceClient _proxy;
        Guid _inventoryCheckingId;
        Guid _assetEntryId;

        AssetInventory assetInventory;

        public CheckingForm(ServiceClient proxy, Guid inventoryCheckingId, Guid assetEntryId)
        {
            InitializeComponent();

            _proxy = proxy;
            _inventoryCheckingId = inventoryCheckingId;
            _assetEntryId = assetEntryId;

            assetStatusDict = new Dictionary<string, AssetStatus>();
            assetStatusDict.Add("在用", AssetStatus.Active);
            assetStatusDict.Add("闲置", AssetStatus.Deactive);
            assetStatusDict.Add("报废", AssetStatus.Scrap);

            checkingResultDict = new Dictionary<string, CheckingResult>();
            checkingResultDict.Add("正常", CheckingResult.Normal);
            checkingResultDict.Add("异常", CheckingResult.Exception);
            checkingResultDict.Add("未盘", CheckingResult.NotStarted);
        }

        private void CheckingForm_Load(object sender, EventArgs e)
        {
            comboBoxAssetStatus.DataSource = assetStatusDict.Keys.ToList();
            comboBoxResult.DataSource = checkingResultDict.Keys.ToList();

            var assetEntry = _proxy.GetAssetEntry(_assetEntryId);
            textBoxAssetEntryId.Text = _assetEntryId.ToString();
            textBoxAssetEntryAppellation.Text = assetEntry.Appellation;

            assetInventory = _proxy.GetAssetInventory(_inventoryCheckingId, _assetEntryId);
            if (assetInventory == null)
            {
                assetInventory = new AssetInventory();
            }
            else
            {
                textBoxQuantity.Text = assetInventory.Quantity.ToString();
                textBoxActualUsePerson.Text = assetInventory.ActualUsePerson;
                if (assetInventory.ActualUseDate.HasValue)
                    dateTimePickerActualUseDate.Value = assetInventory.ActualUseDate.Value;
                else
                    dateTimePickerActualUseDate.Value = DateTime.MinValue;
                textBoxResponsiblePerson.Text = assetInventory.ResponsiblePerson;
                comboBoxAssetStatus.SelectedIndex = assetInventory.AssetStatus;
                comboBoxResult.SelectedIndex = assetInventory.Result;
                textBoxDescription.Text = assetInventory.Description;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            assetInventory.Quantity = Decimal.Parse(textBoxQuantity.Text);
            assetInventory.ActualUsePerson = textBoxActualUsePerson.Text;
            assetInventory.ActualUseDate = dateTimePickerActualUseDate.Value;
            assetInventory.ResponsiblePerson = textBoxResponsiblePerson.Text;
            assetInventory.AssetStatus = (int)assetStatusDict[comboBoxAssetStatus.SelectedItem.ToString()];
            assetInventory.Result = (int)checkingResultDict[comboBoxResult.SelectedItem.ToString()];
            assetInventory.Description = textBoxDescription.Text;

            try
            {
                if (assetInventory.AssetInventoryId != Guid.Empty)
                {
                    _proxy.EditAssetInventory(assetInventory);
                }
                else
                {
                    _proxy.CreateAssetInventory(_inventoryCheckingId, _assetEntryId, assetInventory);
                }

                MessageBox.Show("Success.");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK);
                DialogResult = DialogResult.None;
            }
        }
    }
}
